package com.hualan.sfzy.service.course.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.course.CourseOrder;
import com.hualan.sfzy.mapper.course.CourseOrderMapper;
import com.hualan.sfzy.service.course.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseOrderServiceImpl implements CourseOrderService {
    @Autowired
    private CourseOrderMapper courseOrderMapper;
    @Override
    public int add(CourseOrder courseOrder) {
        courseOrder.setBuyTime(new Date());
        return courseOrderMapper.insert(courseOrder);
    }

    @Override
    public int remove(Integer id) {
        return courseOrderMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return courseOrderMapper.deleteBatch(ids);
    }

    @Override
    public int edit(CourseOrder courseOrder) {
        return courseOrderMapper.update(courseOrder);
    }

    @Override
    public CourseOrder findById(Integer id) {
        return courseOrderMapper.selectById(id);
    }

    @Override
    public List<CourseOrder> find() {
        return courseOrderMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, CourseOrder courseOrder) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseOrder> list = courseOrderMapper.select(courseOrder);
        PageInfo<CourseOrder> pageInfo = new PageInfo<CourseOrder>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    public List<CourseOrder> getCourseOrders(Integer userId, Integer courseId) {
        return courseOrderMapper.selectByUserIdAndCourseId(userId, courseId);
    }
}

